/**
 * Copyright (c) 2018-2028, Joffee.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sailmi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sailmi.common.controller.BaseController;
import com.sailmi.common.model.auth.AuthInfo;
import com.sailmi.common.mybatis.support.Condition;
import com.sailmi.common.tool.api.R;
import com.sailmi.common.tool.node.INode;
import com.sailmi.common.tool.utils.Func;
import com.sailmi.common.tool.utils.SecureUtil;
import com.sailmi.system.entity.Dept;
import com.sailmi.system.service.IDeptService;
import com.sailmi.system.vo.DeptVO;
import com.sailmi.system.wrapper.DeptWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author Joffee
 */
@RestController
@RequestMapping("/dept")
@Tag(name = "部门", description = "部门管理接口")
public class DeptController extends BaseController {

	private final IDeptService deptService;

	public DeptController(IDeptService deptService) {
		this.deptService = deptService;
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入dept")
	public R<DeptVO> detail(Dept dept) {
		Dept detail = deptService.getOne(Condition.getQueryWrapper(dept));
		return R.data(DeptWrapper.build().entityVO(detail));
	}

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@Operation(summary = "列表", description = "传入dept")
	public R<List<INode>> list(@RequestParam Map<String, Object> dept, HttpServletRequest request) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();

		QueryWrapper<Dept> queryWrapper = Condition.getQueryWrapper(dept, Dept.class);
		if(authInfo!=null && authInfo.getEnterprise()!=null){
			queryWrapper.eq("enterprise_id",authInfo.getEnterprise());
		}
		List<Dept> list = deptService.list(queryWrapper);
		return R.data(DeptWrapper.build().listNodeVO(list));
	}

	/**
	 * 获取部门树形结构
	 *
	 * @return
	 */
	@GetMapping("/tree")
	@Operation(summary = "树形结构", description = "树形结构")
	public R<List<DeptVO>> tree(@RequestParam(name="tenantId") String tenantId, HttpServletRequest request) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();

		List<DeptVO> tree = deptService.tree(Func.toStr(tenantId, authInfo.getTenantId()));
		return R.data(tree);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入dept")
	public R submit(@Valid @RequestBody Dept dept,HttpServletRequest request) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();
		if (Func.isEmpty(dept.getId())) {
			dept.setEnterpriseId(authInfo.getTenantId());
		}
		return R.status(deptService.saveOrUpdate(dept));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@Operation(summary = "删除", description = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam(name="ids") String ids) {
		return R.status(deptService.removeByIds(Func.toLongList(ids)));
	}


}
